package com.srd.phone.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.bean.InviteFriends;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.adapter.SuperAdapter;
import com.srd.phone.utils.adapter.internal.SuperViewHolder;
import com.srd.phone.utils.share.ShareDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;

/**
 * 邀请好友
 */
public class InviteFriendsActivity extends BaseActivity implements View.OnClickListener {

    private ListView mListView = null;
    private InviteFriendADapter adapter = null;
    private UtilPresenter utilPresenter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_friends);

        initView();
        initData();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void initView() {
        super.initView();
        tv_common_title.setText("邀请好友");

        tv_common_right.setVisibility(View.VISIBLE);
        tv_common_right.setTextColor(Color.parseColor("#292929"));
        tv_common_right.setText("分享");
        tv_common_right.setOnClickListener(this);

        mListView = (ListView)findViewById(R.id.listview);
        adapter = new InviteFriendADapter(this, new ArrayList<InviteFriends>(), R.layout.item_invite_friends);
        mListView.setAdapter(adapter);
    }

    @Override
    public void initData() {
        super.initData();

        ShareSDK.initSDK(this);
        showWaitingDialog();
        presenter = new UtilPresenter(requestQueue, this);
        utilPresenter = (UtilPresenter)presenter;
        setUrlArrs(Constants.YREDREC_LIST);
        getData();
    }

    private void getData(){
        utilPresenter.noParams(Constants.YREDREC_LIST);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        cancelWaitingDialog();
        if(Constants.YREDREC_LIST.equals(tag)){
            List<InviteFriends> list = InviteFriends.parseData(data);
            adapter.replaceAll(list);
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        cancelWaitingDialog();
    }


    private ShareDialog shareDialog = null;

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_common_right:  // 分享

                shareDialog = new ShareDialog(this, "盛融贷", "盛融贷App上线~","-----", "http://www.baidu.com", new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        //分享成功
//                        setCurrentShareTypeName(platform.getName());
//                        shared(currentShareNameType, currentShareType, currentOrderType, mOrderId);
//                        if (shareDialog != null) {
//                            if (shareDialog.isShowing()) {
//                                shareDialog.dismiss();
//                            }
//                        }
//                        ToastUtils.showToast(SendOrderSucessActivity.this, "分享成功");
                    }


                    @Override
                    public void onError(Platform platform, int i, final Throwable throwable) {

//                        if(throwable.getMessage()==null){
//                            ToastUtils.showToast(SendOrderSucessActivity.this, "未安装该应用");
//                        }else{
//                            ToastUtils.showToast(SendOrderSucessActivity.this, "分享失败"+throwable.getMessage());
//                        }
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
//                        ToastUtils.showToast(SendOrderSucessActivity.this, "取消分享");
//                        Logger.i(Logger.TAG, "取消分享");
                    }
                });
                shareDialog.show();


                break;
        }

    }












//    private ImageView sweepIV;
//
//    private int QR_WIDTH = 200, QR_HEIGHT = 200;
//
//    public void createQRImage(String url)
//    {
//        try
//        {
//            //判断URL合法性
//            if (url == null || "".equals(url) || url.length() < 1)
//            {
//                return;
//            }
//            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
//            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//            //图像数据转换，使用了矩阵转换
//            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
//            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
//            //下面这里按照二维码的算法，逐个生成二维码的图片，
//            //两个for循环是图片横列扫描的结果
//            for (int y = 0; y < QR_HEIGHT; y++)
//            {
//                for (int x = 0; x < QR_WIDTH; x++)
//                {
//                    if (bitMatrix.get(x, y))
//                    {
//                        pixels[y * QR_WIDTH + x] = 0xff000000;
//                    }
//                    else
//                    {
//                        pixels[y * QR_WIDTH + x] = 0xffffffff;
//                    }
//                }
//            }
//            //生成二维码图片的格式，使用ARGB_8888
//            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
//            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
//            //显示到一个ImageView上面
//            sweepIV.setImageBitmap(bitmap);
//        }catch (WriterException e)
//        {
//            e.printStackTrace();
//        }
//    }

    class InviteFriendADapter extends SuperAdapter<InviteFriends> {

        public InviteFriendADapter(Context context, List<InviteFriends> list, int layoutResId) {
            super(context, list, layoutResId);
        }

        @Override
        public void onBind(SuperViewHolder holder, int viewType, int layoutPosition, InviteFriends item) {
            holder.setText(R.id.tv_name, item.username);
            holder.setText(R.id.tv_date, item.ctime);
            holder.setText(R.id.tv_money, item.moneys + "元");
        }
    }

}